"""
@-*- coding: utf-8 -*-
@ python: 3.12.3
@ 创建者: JacksonCode
@ 创建时间: 2025-11-06
@ 描述: 控制器基类，提供统一的响应格式和通用方法
"""
from flask import jsonify
from typing import Any, Dict, Optional, Tuple


class BaseController:
    """控制器基类，提供统一的响应格式和通用方法"""
    
    @staticmethod
    def success_response(
        message: str, 
        data: Any = None, 
        status_code: int = 200
    ) -> Tuple[Dict, int]:
        """
        创建成功响应
        
        Args:
            message: 响应消息
            data: 响应数据
            status_code: HTTP状态码
            
        Returns:
            元组：(响应字典, 状态码)
        """
        response = {
            "code": status_code,
            "message": message,
            "data": data
        }
        return jsonify(response), status_code
    
    @staticmethod
    def error_response(
        message: str, 
        status_code: int = 400, 
        data: Any = None
    ) -> Tuple[Dict, int]:
        """
        创建错误响应
        
        Args:
            message: 错误消息
            status_code: HTTP状态码
            data: 响应数据
            
        Returns:
            元组：(响应字典, 状态码)
        """
        response = {
            "code": status_code,
            "message": message,
            "data": data
        }
        return jsonify(response), status_code
    
    @staticmethod
    def validation_error_response(errors: Dict[str, str]) -> Tuple[Dict, int]:
        """
        创建验证错误响应
        
        Args:
            errors: 验证错误字典，字段名 -> 错误消息
            
        Returns:
            元组：(响应字典, 状态码)
        """
        error_messages = []
        for field, error in errors.items():
            error_messages.append(f"{field}: {error}")
        
        error_message = "验证失败: " + "; ".join(error_messages)
        return BaseController.error_response(error_message, 400, {"errors": errors})
    
    @staticmethod
    def not_found_response(resource: str = "资源") -> Tuple[Dict, int]:
        """
        创建404响应
        
        Args:
            resource: 资源名称
            
        Returns:
            元组：(响应字典, 状态码)
        """
        return BaseController.error_response(f"{resource}不存在", 404)
    
    @staticmethod
    def unauthorized_response(message: str = "未授权访问") -> Tuple[Dict, int]:
        """
        创建401响应
        
        Args:
            message: 错误消息
            
        Returns:
            元组：(响应字典, 状态码)
        """
        return BaseController.error_response(message, 401)
    
    @staticmethod
    def forbidden_response(message: str = "禁止访问") -> Tuple[Dict, int]:
        """
        创建403响应
        
        Args:
            message: 错误消息
            
        Returns:
            元组：(响应字典, 状态码)
        """
        return BaseController.error_response(message, 403)
    
    @staticmethod
    def server_error_response(message: str = "服务器内部错误") -> Tuple[Dict, int]:
        """
        创建500响应
        
        Args:
            message: 错误消息
            
        Returns:
            元组：(响应字典, 状态码)
        """
        return BaseController.error_response(message, 500)